package bb.lanxing.util;

import android.location.Location;
import com.baidu.location.BDLocation;
import com.baidu.mapapi.model.LatLng;
import com.github.mikephil.charting.utils.Utils;

import bb.lanxing.calc.data.CadencePoint;
import bb.lanxing.calc.data.GpsPoint;
import bb.lanxing.calc.data.SourcePoint;
import bb.lanxing.manager.RemoteSharedPreference;
import bb.lanxing.manager.SPConstant;
import bb.lanxing.model.database.Trackpoint;
import bb.lanxing.lib.kt.model.geo.IGeoPoint;
import bb.lanxing.model.database.Workout;

public class DistanceUtil {
    private static double EARTH_RADIUS = 6378137.0d;

    private static double rad(double d) {
        return (d * 3.141592653589793d) / 180.0d;
    }

    public static double get(double lat1, double lng1, double lat2, double lng2) {
        double rad = rad(lat1);
        double rad2 = rad(lat2);
        return Math.round(((Math.asin(Math.sqrt(Math.pow(Math.sin((rad - rad2) / 2.0d), 2.0d) + ((Math.cos(rad) * Math.cos(rad2)) * Math.pow(Math.sin((rad(lng1) - rad(lng2)) / 2.0d), 2.0d)))) * 2.0d) * EARTH_RADIUS) * 10000.0d) / 10000.0d;
    }

    public static double get(BDLocation loc1, BDLocation loc2) {
        return (loc1 == null || loc2 == null) ? Utils.DOUBLE_EPSILON : get(loc1.getLatitude(), loc1.getLongitude(), loc2.getLatitude(), loc2.getLongitude());
    }

    public static double get(LatLng loc1, LatLng loc2) {
        return get(loc1.latitude, loc1.longitude, loc2.latitude, loc2.longitude);
    }

    public static double get(Location loc1, Location loc2) {
        return (loc1 == null || loc2 == null) ? Utils.DOUBLE_EPSILON : get(loc1.getLatitude(), loc1.getLongitude(), loc2.getLatitude(), loc2.getLongitude());
    }

//    public static double get(GpsData gp1, GpsData gp2) {
//        return (gp1 == null || gp2 == null) ? Utils.DOUBLE_EPSILON : get(gp1.getLatitude(), gp1.getLongitude(), gp2.getLatitude(), gp2.getLongitude());
//    }

    public static double get(SourcePoint sp1, SourcePoint sp2) {
        return (sp1 == null || sp2 == null) ? Utils.DOUBLE_EPSILON : get(sp1.getGpsPoint(), sp2.getGpsPoint());
    }

    public static double get(GpsPoint gp1, GpsPoint gp2) {
        return (gp1 == null || gp2 == null) ? Utils.DOUBLE_EPSILON : get(gp1.getLatitude(), gp1.getLongitude(), gp2.getLatitude(), gp2.getLongitude());
    }

    public static double get(IGeoPoint gp1, IGeoPoint gp2) {
        return (gp1 == null || gp2 == null) ? Utils.DOUBLE_EPSILON : get(gp1.getLatitude(), gp1.getLongitude(), gp2.getLatitude(), gp2.getLongitude());
    }

    public static double get(Trackpoint loc1, Trackpoint loc2) {
        return get(loc1.getLatitude(), loc1.getLongitude(), loc2.getLatitude(), loc2.getLongitude());
    }

    public static double getWithCadence(SourcePoint sp1, SourcePoint sp2) {
        return getWithCadence(sp1.getCadencePoint(), sp2.getCadencePoint());
    }

    public static double getWithCadence(CadencePoint cp1, CadencePoint cp2) {
        float f;
        float f2;
        if (cp1 != null && cp2 != null) {
            int sumWheel = cp1.getSumWheel();
            int sumWheel2 = cp2.getSumWheel();
            if (sumWheel > 0 && sumWheel2 > 0) {
                int abs = Math.abs(sumWheel - sumWheel2);
                if (cp1.getSource() == Workout.CADENCE_SOURCE_BICI) {
                    f = abs;
                    f2 = 1.593f;
                } else {
                    f = abs;
                    f2 = RemoteSharedPreference.getInstance().getFloat(SPConstant.KEY_WHEEL_LENGTH, 2.171f);
                }
                return f * f2;
            }
        }
        return Utils.DOUBLE_EPSILON;
    }
}